package com.jic.market.impl.skill;

import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.market.entity.skill.ActivityMerchantConfig;
import com.jic.market.mapper.skill.ActivityMerchantConfigMapper;
import com.jic.market.request.CouponDetailRequest;
import com.jic.market.request.manager.base.PromotionIdRequest;
import com.jic.market.service.skill.ActivityMerchantConfigService;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * 商家配置表
 *
 * @author : seven
 * @version : v1.0.0
 * @email : 774329481@qq.com
 * @since : 2020-03-12 04:52:22
 */
@Service
public class ActivityMerchantConfigServiceImpl implements ActivityMerchantConfigService {

    @Autowired
    private ActivityMerchantConfigMapper activityMerchantConfigMapper;

    @Override
    public long insert(ActivityMerchantConfig activityMerchantConfig) {
        activityMerchantConfig.setDeleteFlag(false);
        long flag = activityMerchantConfigMapper.insert(activityMerchantConfig);
        return flag;
    }

    @Override
    public long delete(String promotionId) {
        return activityMerchantConfigMapper.delete(promotionId);
    }

    @Override
    public long update(ActivityMerchantConfig activityMerchantConfig) {
        return activityMerchantConfigMapper.update(activityMerchantConfig);
    }

    @Override
    public ActivityMerchantConfig queryByPrimaryKey(Long id) {
        return activityMerchantConfigMapper.queryByPrimaryKey(id);
    }

    @Override
    public List<ActivityMerchantConfig> query(ActivityMerchantConfig activityMerchantConfig) {
        return activityMerchantConfigMapper.query(activityMerchantConfig);
    }

    @Override
    public PageResult<ActivityMerchantConfig> queryPage(ActivityMerchantConfig activityMerchantConfig, Page pageQuery) {
        //计算下标
        int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
        List<ActivityMerchantConfig> list = activityMerchantConfigMapper.queryPage(startIndex, pageQuery.getLimit(), activityMerchantConfig);
        long count = activityMerchantConfigMapper.queryPageCount(activityMerchantConfig);
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }

    @Override
    public List<ActivityMerchantConfig> queryByPromotionId(String promotionId, Integer start , Integer limit, List<Long> authMerchantIds, CouponDetailRequest promotionIdRequest) {
        if(start != null && limit != null){
            start = (start - 1) * limit;
        }
        return activityMerchantConfigMapper.queryByPromotionId(promotionId, start ,limit,authMerchantIds,promotionIdRequest);
    }

    @Override
    public long deleteByPromotionIdAndMerchantId(String promotionId, String merchantId) {
        return activityMerchantConfigMapper.deleteByPromotionIdAndMerchantId(promotionId,
                merchantId);
    }

    @Override
    public boolean copyMerchant(PromotionIdRequest promotionIdRequest, String newPromotionId) {
        List<ActivityMerchantConfig> merchantConfigList = queryByPromotionId(promotionIdRequest.getPromotionId(), null,null, Collections.emptyList(), null);
        if (CollectionUtils.isEmpty(merchantConfigList)) {
            return true;
        }
        merchantConfigList.stream().forEach(temp -> {
            temp.setPromotionId(newPromotionId);
            temp.setDeleteFlag(false);
            //insert(temp);
        });
        activityMerchantConfigMapper.insertBatch(merchantConfigList);
        return true;
    }

    @Override
    public Long queryByPromotionIdTotal(String promotionId,List<Long> authMerchantIds, CouponDetailRequest promotionIdRequest) {
        return activityMerchantConfigMapper.queryByPromotionIdTotal(promotionId,authMerchantIds,promotionIdRequest);
    }

    @Override
    public List<Long> queryMerchantIdsByPromotionId(String promotionId) {
        return activityMerchantConfigMapper.queryMerchantIdsByPromotionId(promotionId);
    }
}